import React, { Component } from 'react'
import { Modal,Input, message} from 'antd'
import axios from 'axios'
export default class Updata extends Component {
    state={
        "id":this.props.list.id,
        "classifyName": this.props.list.classifyName,
        list:{}
    }
    static getDerivedStateFromProps(props, state) {
        const {list} = props
        //如果是提交 不要让父组件props更新传过来
        if (state.setStateOccurs) {
            state = {...state,setStateOccurs: false}
            return state
        } else {
            state = { ...state, list,}
            return state;
		}
	}
    //框内改值
    getVal=(e)=>{
		let list = this.state.list
		list[e.target.name] = e.target.value
		this.setState({
			list,
			setStateOccurs: true 
		})
        console.log("@@",this.state)
    }
    // 敲击回车键进行登录
    handleKeyUp =(event)=>{
        // console.log("@")
    // 结构赋值获取keyCode,target
    const {keyCode} = event
    // 判断keyCode是否是回车
    if(keyCode !== 13) return
    // 执行add方法
    this.update()
    }
    update=()=>{
        axios.post("/api/payment/classify/update",{
            "id":this.state.id,
            "classifyName": this.state.list.classifyName,
        }).then(res=>{
            console.log(res)
            if(res.data.code===200){
                message.success(res.data.msg,1,()=>{
                    this.props.closeUpdate()
                })
            }
            console.log("@",this.state)
        })
    }
  render() {
      const {updateVisible} = this.props
    return (
      <div>
          <Modal title="修改信息" visible={updateVisible} okText={"确定"} cancelText={"取消"} onCancel={this.props.closeUpdate} onOk={this.update}>
              <Input style={{height: "50px",marginBottom:" 20px",borderRadius: "5px"}} type="text" name="classifyName" value={this.state.list.classifyName} onChange={this.getVal} onKeyUp={this.handleKeyUp}></Input>
          </Modal>
      </div>
    )
  }
}
